package com.fanco.learn_datastruct_leetcode.juc;/**
 * @author qiaowang 2021/8/17
 */

import java.util.concurrent.atomic.LongAdder;

/**
 * @author fanco
 * @Desc 描述
 */
class Resource {
    private LongAdder num = new LongAdder();

    public LongAdder getNum() {
        return num;
    }

    public void add() {
        num.increment();
    }
}
public class LongAdderDemo {
    public static void main(String[] args) {
        Resource resource = new Resource();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    resource.add();
                }
//                System.out.println(Thread.currentThread().getName() + " 线程启动");

            }, "线程" + i).start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(resource.getNum());

    }
}
